// pages/authentication/shop/index.js
const rules = require('../../../utils/util.js')
const uploadImage = require("../../../utils/uploadImg/uploadImg.js");
const service = require('../../../service/authentication')
Page({

  /**
   * 页面的初始数据
   */
  data: {

    idcardfront: [],
    idcardback: [],

    name: "",
    Contact: "",
    phone: "",
    identity: "",
    nameErrorText: "",
    ContactErrorText: "",
    phoneErrorText: "",
    identityErrorText: ""
  },
  blurEvent(e) {

    if (e.detail.value == '') {
      this.setData({
        nameErrorText: '所属企业名称不能为空！',
      });
    } else {
      this.setData({
        nameErrorText: '',
        name: e.detail.value
      });
    }
  },
  blurEventContact(e) {

    if (e.detail.value == '') {
      this.setData({
        ContactErrorText: '主播姓名不能为空！',
      });
    } else {
      this.setData({
        ContactErrorText: '',
        Contact: e.detail.value
      });
    }
  },
  blurEventphone(e) {

    if (e.detail.value == '') {
      this.setData({
        phoneErrorText: '主播联系手机不能为空！',
      });
    } else {
      this.setData({
        phoneErrorText: '',
        phone: e.detail.value
      });
    }
  },
  blurEventidentity(e) {
    if (e.detail.value == '') {
      this.setData({
        identityErrorText: '身份证号不能为空！',
      });
    } else {
      this.setData({
        identityErrorText: '',
        identity: e.detail.value
      });
    }
  },
  onChangephone(e) {
    //console.log(e.detail)
    if (!rules.regPhone(e.detail)) {
      this.setData({
        phoneErrorText: '手机号格式有误！',
      });
    } else {
      this.setData({
        phoneErrorText: '',
      });
    }
  },
  onChangeidentity(e) {
    //console.log(e.detail)
    if (!rules.regIdentity(e.detail)) {
      this.setData({
        identityErrorText: '身份证号格式有误！',
      });
    } else {
      this.setData({
        identityErrorText: '',
      });
    }
  },
  afterReadidcardf: function (e) {
    // console.log(e);
    var list = this.data.idcardfront;

    list.push({
      url: e.detail.file.path,
    });
    this.setData({
      idcardfront: list,

    });
  },
  afterReadidcardb: function (e) {
    // console.log(e);
    var list = this.data.idcardback;

    list.push({
      url: e.detail.file.path,
    });
    this.setData({
      idcardback: list,

    });
  },
  deletephotoidcardf: function (event) {
    const {
      index,
      name
    } = event.detail;
    const fileList = this.data.idcardfront;
    fileList.splice(index, 1);
    this.setData({
      idcardfront: fileList,
    });
  },
  deletephotoidcardb: function (event) {
    const {
      index,
      name
    } = event.detail;
    const fileList = this.data.idcardback;
    fileList.splice(index, 1);
    this.setData({
      idcardback: fileList,
    });
  },
  submit() {
    if (this.data.name == "") {
      this.setData({
        nameErrorText: '所属企业名称不能为空！',
      });

      return
    }
    if (this.data.Contact == "") {
      this.setData({
        ContactErrorText: '主播姓名不能为空！',
      });

      return
    }
    if (this.data.phone == "") {
      this.setData({
        phoneErrorText: '主播联系手机不能为空！',
      });

      return
    }
    if (this.data.identity == "") {
      this.setData({
        identityErrorText: '主播联系手机不能为空！',
      });

      return
    }
    if (!this.data.idcardfront[0]) {
      wx.showToast({
        title: '请上传身份证正面照片！',
        icon:"none"
      })
      return
    }
    if (!this.data.idcardback[0]) {
      wx.showToast({
        title: '请上传身份证反面照片！',
        icon:"none"
      })
      return
    }
    wx.showLoading({
      title: '加载中...',
    })
    service.addanchor({
      "customerid":wx.getStorageSync('userInfo').id,
      //所属企业名称
      "name": this.data.name,
      //主播姓名
      "anchorName": this.data.Contact,
      //主播电话
      "anchorPhone": this.data.phone,
      //主播身份证号
      "idcardNum": this.data.identity,
      //身份证正面照片地址
      "anchorIdcard": this.data.idcardfront[0]?this.data.idcardfront[0].url:'',
      //身份证反面照片
      "anchorIdcardRebellion": this.data.idcardback[0]?this.data.idcardback[0].url:''

    }, res => {
      if (res.code == "00000") {
        wx.redirectTo({
          url: '/pages/authentication/index',
        })
      } else {
        wx.showToast({
          title: '请重新提交',
          icon:'none',
          duration:5000
        })
      }
      wx.hideLoading()
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})